
function assignActivity(iObjType)
{
var sEntityId = new Array(crmFormSubmit.crmFormSubmitId.value);
var sPath = prependOrgName("/_grid/cmds/dlg_assignqueue.aspx?iObjType=" + CrmEncodeDecode.CrmUrlEncode(iObjType) + "&iTotal=1&sQType=1&FromForm=1");

var oReturnValue = openStdDlg(sPath, sEntityId, 500, 310);

if (oReturnValue && oReturnValue.OwnerId)
{

var lookupItem = new LookupControlItem(oReturnValue.OwnerId, oReturnValue.OwnerType, oReturnValue.OwnerName);
var lookupItems = new Array();
lookupItems.push(lookupItem);

crmForm.ownerid.Clear();
crmForm.ownerid.AddItems(lookupItems);

var bFormSubmitted = true;
if (crmForm.BuildXml(false, false) == 3)
{

createHiddenInput("assignOwnerId", oReturnValue.OwnerId);
createHiddenInput("assignOwnerType", oReturnValue.OwnerType);



bFormSubmitted = crmForm.SubmitCrmForm(47, true, true, false);
}
else
{



bFormSubmitted = crmForm.SubmitCrmForm(1, true, true, false);
}

if (bFormSubmitted)
{
RefreshParentGrid(iObjType);
}
}
}



function assignSpecificActivity(iObjType, sSrcQueueId)
{
if (!crmForm.IsValid())
{
return;
}

var sEntityId = new Array(crmFormSubmit.crmFormSubmitId.value);
var sPath = prependOrgName("/_grid/cmds/dlg_assignqueue.aspx?iObjType=" + CrmEncodeDecode.CrmUrlEncode(iObjType) + "&iTotal=1&sQType=1&srcQueueId=" + CrmEncodeDecode.CrmUrlEncode(sSrcQueueId));

openStdDlg(sPath, sEntityId, 500, 310);





crmForm.SubmitCrmForm(4, true, true, false);

RefreshParentGrid(iObjType);
}


function SaveAsCompleted()
{
if (!crmForm.IsValid())
{
return;
}




crmForm.SubmitCrmForm(58, true, true, false);
}


function RefreshParentGrid(iObjType)
{
try
{
window.opener.auto(iObjType);
}
catch(e)
{}
}









function SwapLookups(oLookupA, oLookupB)
{
var swapStyle = "single";


if (oLookupA.lookupstyle.toLowerCase() == "multi" &&
oLookupB.lookupstyle.toLowerCase() == "multi")
{
swapStyle = "multi";
}


var aoLookupAItems = oLookupA.Items();
var aoLookupBItems = oLookupB.Items();


if (swapStyle == "single" && (aoLookupAItems.length > 1 || aoLookupBItems.length > 1))
{

if (!openStdDlg(prependOrgName("/Activities/dlg_confirm_swap.aspx"), null, 350, 250))
{
return false;
}


aoLookupAItems = TrimLookup(aoLookupAItems);
aoLookupBItems = TrimLookup(aoLookupBItems);
}


oLookupA.Clear();
oLookupA.AddItems(aoLookupBItems);

oLookupB.Clear();
oLookupB.AddItems(aoLookupAItems);

return true;
}




function TrimLookup(aoLookupItems)
{

if (aoLookupItems.length > 1)
{
var aoReturnItem = new Array();
aoReturnItem.push(aoLookupItems[0]);
return aoReturnItem;
}
else
{
return aoLookupItems;
}
}


function promoteToResponse(iObjType)
{
if (crmForm.IsValid())
{
var sUrl = "?_CreateFromType=" + CrmEncodeDecode.CrmUrlEncode(iObjType) + "&_CreateFromId=" + CrmEncodeDecode.CrmUrlEncode(crmFormSubmit.crmFormSubmitId.value);
openFrmObj(sUrl, buildWinName(), CampaignResponse);
}
}


function convertToCase(iObjType)
{

if (crmForm.IsDirty)
{
alert(LOCID_CONV_ACT_SAVE_WARNING);


return false;
}

if (crmForm.IsValid())
{

var directionCodeCtrl = crmForm.all.directioncode;
var directionCodeVal = true;


if (!IsNull(directionCodeCtrl))
{
directionCodeVal = directionCodeCtrl.DataValue;
}

if (IsNull(directionCodeVal))
{
directionCodeCtrl = document.getElementById("directioncode");
if (!IsNull(directionCodeCtrl))
{
directionCodeVal = directionCodeCtrl.DataValue;
directionCodeVal = directionCodeVal == "0" ? false : true;
}
}

var sUrlElement = "";
var subjectField = "";

if (!IsNull(crmForm.all.subject.value))
{
subjectField = crmForm.all.subject.value;
}
sUrlElement += "subject=" + CrmEncodeDecode.CrmUrlEncode(subjectField);


var customerItems = null;
var secondaryCust = null;
sUrlElement += "&activityType=" + CrmEncodeDecode.CrmUrlEncode(iObjType);

if (directionCodeVal == true)
{

if (!IsNull(crmForm.all.to))
{
customerItems = crmForm.all.to.Items();
}
if (!IsNull(crmForm.all.cc))
{
secondaryCust = crmForm.all.cc.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
if (!IsNull(crmForm.all.bcc))
{
secondaryCust = crmForm.all.bcc.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
}
else if (directionCodeVal == false)
{

if (!IsNull(crmForm.all.from))
{
customerItems = crmForm.all.from.Items();
}
}


if (iObjType == Appointment)
{
if (!IsNull(crmForm.all.requiredattendees))
{
customerItems = crmForm.all.requiredattendees.Items();
}
if (!IsNull(crmForm.all.optionalattendees))
{
secondaryCust = crmForm.all.optionalattendees.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
}

var isCustomerPresent = false;
var isLeadPresent = false;

if (!IsNull(customerItems))
{
for(var i=0; i<customerItems.length; i++)
{

if (eval(customerItems[i].type) == Account || eval(customerItems[i].type) == Contact)
{
try
{

var command = new RemoteCommand("ActivitiesWebService", "IsActiveRecord");


command.SetParameter("objectId", customerItems[i].id);
command.SetParameter("objectTypeCode", customerItems[i].type);


var oResult = command.Execute();

if (oResult.Success)
{

var isActive = oResult.ReturnValue;

if (isActive == "1")
{
sUrlElement += "&customerId=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].id) +
"&customerType=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].type) +
"&customerName=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].name);
isCustomerPresent = true;
break;
}
}
}
catch(e)
{
}
}
}
}

var height = 320;


var objRet = openStdDlg(prependOrgName("/Activities/act_dlgs/convert_to_case.aspx?" + sUrlElement), null, 410, height);


if (objRet)
{

try
{

var command = new RemoteCommand("ActivitiesWebService", "ConvertActivityToCase");


command.SetParameter("activitySubject", CrmEncodeDecode.CrmXmlEncode(crmForm.all.subject.value));
command.SetParameter("customerId", objRet.CustomerId);
command.SetParameter("customerType", objRet.CustomerType);
command.SetParameter("subjectId", objRet.SubjectId);
command.SetParameter("subjectType", objRet.SubjectType);


var oResult = command.Execute();

if (oResult.Success)
{

var caseId = oResult.ReturnValue;

if (caseId != "" && !IsNull(crmForm.GetControl("regardingobjectid")))
{
var itemsArray = new Array();
var reg = new LookupControlItem();

reg.id = caseId;
reg.type = Incident;
if (!IsNull(crmForm.all.subject))
{
reg.name = crmForm.all.subject.value;
}
else
{
reg.name = "";
}
itemsArray.push(reg);

crmForm.GetControl("regardingobjectid").IsDirty = true;
crmForm.GetControl("regardingobjectid").DataValue = itemsArray;
}

if (objRet.SaveActivity == false)
{
if (caseId != "" && !IsNull(crmForm.GetControl("regardingobjectid")))
{

crmForm.SubmitCrmForm(1, true, true, false);
}
}
else
{

var oCrNewState = createHiddenInput("newStateCode", 1);


if (!crmForm.SubmitCrmForm(5, true, true, false));
{

deleteInput(oCrNewState);
}
}
if (objRet.SaveActivity == true)
{

window.close();
}
RefreshParentGrid(iObjType);

if (objRet.OpenNewRecord && caseId != "")
{
openObj(Incident, caseId);
}

}
}
catch(e)
{
}
}
}

RefreshParentGrid(iObjType);
}


function convertToLead(iObjType)
{

if (crmForm.IsDirty)
{
alert(LOCID_CONV_ACT_SAVE_WARNING);


return false;
}

if (!crmForm.IsValid())
{
return false;
}

var sUrlElement = "activityType=" + CrmEncodeDecode.CrmUrlEncode(iObjType);
sUrlElement += "&activityId=" + CrmEncodeDecode.CrmUrlEncode(crmFormSubmit.crmFormSubmitId.value);


var height = 390;
var objRet = openStdDlg(prependOrgName("/Activities/act_dlgs/convert_to_lead.aspx?" + sUrlElement), null, 410, height);


if (objRet)
{

try
{

var command = new RemoteCommand("ActivitiesWebService", "ConvertActivityToLead");


command.SetParameter("activityIdStr", crmFormSubmit.crmFormSubmitId.value);
command.SetParameter("activityType", iObjType);
command.SetParameter("topic", CrmEncodeDecode.CrmXmlEncode(objRet.Topic));
command.SetParameter("firstName", CrmEncodeDecode.CrmXmlEncode(objRet.FirstName));
command.SetParameter("lastName", CrmEncodeDecode.CrmXmlEncode(objRet.LastName));
command.SetParameter("company", CrmEncodeDecode.CrmXmlEncode(objRet.Company));
command.SetParameter("emailAddress", objRet.EmailAddress);



var oResult = command.Execute();

if (oResult.Success)
{

if (objRet.SaveActivity == true)
{

window.close();
}
else
{
crmForm.SubmitCrmForm(1, true, true, false);
}

RefreshParentGrid(iObjType);


var leadId = oResult.ReturnValue;


if (objRet.OpenNewRecord && leadId != "")
{
openObj(Lead, leadId);
}
}
}
catch(e)
{
}
}
}


function convertToOpportunity(iObjType)
{

if (crmForm.IsDirty)
{
alert(LOCID_CONV_ACT_SAVE_WARNING);


return false;
}

if (crmForm.IsValid())
{

var directionCodeCtrl = crmForm.all.directioncode;
var directionCodeVal = true;


if (!IsNull(directionCodeCtrl))
{
directionCodeVal = directionCodeCtrl.DataValue;
}

if (IsNull(directionCodeVal))
{
directionCodeCtrl = document.getElementById("directioncode");
if (!IsNull(directionCodeCtrl))
{
directionCodeVal = directionCodeCtrl.DataValue;
directionCodeVal = directionCodeVal == "0" ? false : true;
}
}

var customerItems = null;
var secondaryCust = null;
var sUrlElement = "";
sUrlElement += "activityType=" + CrmEncodeDecode.CrmUrlEncode(iObjType);

if (directionCodeVal == true)
{

if (!IsNull(crmForm.all.to))
{
customerItems = crmForm.all.to.Items();
}
if (!IsNull(crmForm.all.cc))
{
secondaryCust = crmForm.all.cc.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
if (!IsNull(crmForm.all.bcc))
{
secondaryCust = crmForm.all.bcc.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
}
else if (directionCodeVal == false)
{

if (!IsNull(crmForm.all.from))
{
customerItems = crmForm.all.from.Items();
}
}


if (iObjType == Appointment)
{
if (!IsNull(crmForm.all.requiredattendees))
{
customerItems = crmForm.all.requiredattendees.Items();
}
if (!IsNull(crmForm.all.optionalattendees))
{
secondaryCust = crmForm.all.optionalattendees.Items();
for(var i=0; i<secondaryCust.length; i++)
{
customerItems[customerItems.length] = secondaryCust[i];
}
}
}

var isCustomerPresent = false;
var isLeadPresent = false;

if (!IsNull(customerItems))
{
for(var i=0; i<customerItems.length; i++)
{

if (eval(customerItems[i].type) == Account || eval(customerItems[i].type) == Contact)
{
try
{

var command = new RemoteCommand("ActivitiesWebService", "IsActiveRecord");


command.SetParameter("objectId", customerItems[i].id);
command.SetParameter("objectTypeCode", customerItems[i].type);


var oResult = command.Execute();

if (oResult.Success)
{

var isActive = oResult.ReturnValue;

if (isActive == "1")
{
sUrlElement += "&customerId=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].id) +
"&customerType=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].type) +
"&customerName=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].name);
isCustomerPresent = true;
break;
}
}
}
catch(e)
{
}
}
}

if (isCustomerPresent == false)
{
for(var i=0; i<customerItems.length; i++)
{

if (eval(customerItems[i].type) == Lead)
{
try
{

var command = new RemoteCommand("ActivitiesWebService", "IsActiveRecord");


command.SetParameter("objectId", customerItems[i].id);
command.SetParameter("objectTypeCode", customerItems[i].type);


var oResult = command.Execute();

if (oResult.Success)
{

var isActive = oResult.ReturnValue;

if (isActive == "1")
{
sUrlElement += "&leadId=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].id) +
"&leadName=" + CrmEncodeDecode.CrmUrlEncode(customerItems[i].name);
isLeadPresent = true;
break;
}
}
}
catch(e)
{
}
}
}
}
}


var regardingCtrl = crmForm.all.regardingobjectid;
if (!IsNull(regardingCtrl))
{

var regardingItems = regardingCtrl.Items();


if (!IsNull(regardingItems) && regardingItems.length == 1)
{

if ((eval(regardingItems[0].type) == CampaignActivity) || (eval(regardingItems[0].type) == BulkOperation))
{
sUrlElement += "&campaignId=" + CrmEncodeDecode.CrmUrlEncode(regardingItems[0].id) +
"&campaignType=" + CrmEncodeDecode.CrmUrlEncode(regardingItems[0].type) +
"&campaignName=" + CrmEncodeDecode.CrmUrlEncode(regardingItems[0].name);
}
}
}

var subjectField = "";

if (!IsNull(crmForm.all.subject.value))
{
subjectField = crmForm.all.subject.value;
}
var ownerId = "";
var ownerType = "";

if (!IsNull(crmForm.all.ownerid.DataValue))
{
ownerId = crmForm.all.ownerid.DataValue[0].id;
ownerType = crmForm.all.ownerid.DataValue[0].type;
}
sUrlElement += "&subject=" + CrmEncodeDecode.CrmUrlEncode(subjectField);
sUrlElement += "&ownerId=" + CrmEncodeDecode.CrmUrlEncode(ownerId);
sUrlElement += "&ownerType=" + CrmEncodeDecode.CrmUrlEncode(ownerType);

var height = 350;
if (isLeadPresent == true)
{
height += 30;
}


var objRet = openStdDlg(prependOrgName("/Activities/act_dlgs/convert_activity.aspx?" + sUrlElement), null, 410, height);


if (objRet)
{

try
{

var command = new RemoteCommand("ActivitiesWebService", "ConvertActivity");


command.SetParameter("activitySubject", CrmEncodeDecode.CrmXmlEncode(crmForm.all.subject.value));
command.SetParameter("activityId", crmFormSubmit.crmFormSubmitId.value);
command.SetParameter("activityType", iObjType);
command.SetParameter("leadId", objRet.LeadId);
command.SetParameter("customerId", objRet.CustomerId);
command.SetParameter("customerType", objRet.CustomerType);
command.SetParameter("currencyId", objRet.CurrencyId);
command.SetParameter("campaignId", objRet.CampaignId);
command.SetParameter("campaignType", objRet.CampaignType);
command.SetParameter("logResponse", objRet.LogResponse);
command.SetParameter("ownerId", objRet.ownerId);
command.SetParameter("ownerType", objRet.ownerType);


var oResult = command.Execute();

if (oResult.Success)
{

var oppId = oResult.ReturnValue;

if (oppId != "" && !IsNull(crmForm.GetControl("regardingobjectid")))
{
var itemsArray = new Array();
var reg = new LookupControlItem();

reg.id = oppId;
reg.type = Opportunity;
if (!IsNull(crmForm.all.subject))
{
reg.name = crmForm.all.subject.value;
}
else
{
reg.name = "";
}
itemsArray.push(reg);

crmForm.GetControl("regardingobjectid").IsDirty = true;
crmForm.GetControl("regardingobjectid").DataValue = itemsArray;
}

if (objRet.SaveActivity == false)
{
if (oppId != "" && !IsNull(crmForm.GetControl("regardingobjectid")))
{

crmForm.SubmitCrmForm(1, true, true, false);
}
}
else
{

var oCrNewState = createHiddenInput("newStateCode", 1);


if (!crmForm.SubmitCrmForm(5, true, true, false));
{

deleteInput(oCrNewState);
}
}

if (objRet.OpenNewRecord && oppId != "")
{
openObj(Opportunity, oppId);
}
if (objRet.SaveActivity == true)
{

window.close();
}
RefreshParentGrid(iObjType);
}
}
catch(e)
{
}
}
}

RefreshParentGrid(iObjType);
}



function okToSave()
{
if (crmForm.IsDirty && !IsNull(crmForm.statecode) && (crmForm.statecode.value == 1 || crmForm.statecode.value == 2))
{
if (!confirm(LOCID_ACTIVITY_CLOSED_WARNING))
{

return false;
}
}
return true;
}

function CustomActivitySubmit()
{
if ((typeof(event.returnValue) == "undefined") || (event.returnValue != false))
{
event.returnValue = okToSave();
}
}

function UnsubscribeLink()
{

var sText = document.selection.createRange();











document.execCommand("CreateLink", false, "mailto:[Run_Time_Address]?subject=[Run_Time_Subject]");
}
